package com.prs.crm.action.sale;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.domain.sale.GatheringWay;
import com.prs.crm.domain.sale.InvoiceType;
import com.prs.crm.domain.sale.SalesContract;
import com.prs.crm.domain.sale.SalesContractDetail;
import com.prs.crm.domain.sale.SalesShipping;
import com.prs.crm.domain.sale.SalesShippingDetail;
import com.prs.crm.domain.sale.ShippingWay;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.sale.GatheringWayService;
import com.prs.crm.service.sale.InvoiceTypeService;
import com.prs.crm.service.sale.SalesShippingService;
import com.prs.crm.service.sale.ShippingWayService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreRoomService;

public class SalesShippingAction extends FlowAction<SalesShipping> {

	private static final long serialVersionUID = 1L;

	/**
	 * 产品详细
	 */
	private Product[] products;

	private String[] pronames;

	private Double[] originalCosts;

	private Double[] nowCosts;

	private Integer[] returnsNums;

	private Double[] rebates;

	private Double[] totalPrices;

	private SalesContract[] conratcts;

	private SalesShippingDetail salesShippingDetail;

	private SalesShipping salesShipping;

	private Product product;

	private StoreRoom room;

	@Autowired
	private ProductStoreService productStoreService;

	private SalesShippingService salesShippingService;

	private GatheringWayService gatheringWayService;

	private ShippingWayService shippingWayService;

	private InvoiceTypeService invoiceTypeService;

	private StoreRoomService storeRoomService;

	private String states;

	private String proName;

	private String batchNumber;

	private PaginationSupport pagination;
	/**
	 * 收款方式
	 */
	private List<GatheringWay> gatheringWaylist;
	/**
	 * 发货方式
	 */
	private List<ShippingWay> shippingWaylist;
	/**
	 * 发票类型
	 */
	private List<InvoiceType> invoiceTypelist;

	/**
	 * 库房
	 * 
	 * @return
	 */
	private List<StoreRoom> roomlist;

	private List<SalesShippingDetail> Detailist;

	private Collection<SalesShippingDetail> slist;
	private String flow;

	@FlowActive(target = "salesShipping", flowservice = "salesShippingService")
	@Action("editSalesShipping")
	public String editSalesShipping() {
		this.gatheringWaylist = this.getGatheringWayService()
				.getAllGatheringWay();
		this.shippingWaylist = this.getShippingWayService().getAllShippingWay();
		this.invoiceTypelist = this.getInvoiceTypeService().getAllInvoiceType();
		this.roomlist = this.getStoreRoomService().getAllstoreroom();
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(target = "salesShipping", flowservice = "salesShippingService")
	@Action(value = "saveSalesShipping", results = {
			@Result(name = "success", type = "redirect", location = "listSalesShipping"),
			@Result(name = "error", type = "chain", location = "editSalesShipping"),
			@Result(name = "none", type = "redirect", location = "listSalesShipping") })
	public String saveSalesShipping() {
		if (flow == null || flow.equals("")) {
			this.addActionError("请添加审核流程");
			return ERROR;
		}
		if (salesShipping.getSalesContract() == null) {
			this.addActionError("请添加关联合同");
			return ERROR;
		}
		if (salesShipping.getSalesShippingName() == null
				|| salesShipping.getSalesShippingName().equals("")) {
			this.addActionError("请添加发货主题");
			return ERROR;
		}
		if (salesShipping.getGatheringDate() == null) {
			this.addActionError("请添加收款期限");
			return ERROR;
		}
		if (salesShipping.getShippingCode() == null) {
			this.addActionError("请添加发货日期");
			return ERROR;
		}
		if (salesShipping.getReachSite() == null
				|| salesShipping.getReachSite().equals("")) {
			this.addActionError("请添加到站地点");
			return ERROR;
		}
		if (salesShipping.getShippingCode() == null
				|| salesShipping.getShippingCode().equals("")) {
			this.addActionError("请添加单号");
			return ERROR;
		}
		if (salesShipping.getBuySite() == null
				|| salesShipping.getBuySite().equals("")) {
			this.addActionError("请添加单位名称");
			return ERROR;
		}
		if (salesShipping.getPerson() == null
				|| salesShipping.getPerson().equals("")) {
			this.addActionError("请添加联系人");
			return ERROR;
		}
		if (salesShipping.getAddress() == null
				|| salesShipping.getAddress().equals("")) {
			this.addActionError("请添加通讯地址");
			return ERROR;
		}
		if (salesShipping.getPhone() == null
				|| salesShipping.getPhone().equals("")) {
			this.addActionError("请添加电话");
			return ERROR;
		}
		if (salesShipping.getBankNum() == null
				|| salesShipping.getBankNum().equals("")) {
			this.addActionError("请添加银行账号");
			return ERROR;
		}
		if (salesShipping.getLevyNum() == null
				|| salesShipping.getLevyNum().equals("")) {
			this.addActionError("请添加税号");
			return ERROR;
		}
		if (products == null) {
			this.addActionError("产品不能为空");
			return ERROR;
		}
		this.getSalesShippingService().saveSalesShipping(salesShipping,
				products, pronames, originalCosts, nowCosts, returnsNums,
				rebates, totalPrices, conratcts);
		this.setPrompt("操作成功");
		return SUCCESS;
	}

	@Action("listSalesShipping")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SalesShipping.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSalesShippingService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewSalesShipping")
	public String viewSalesShipping() {
		salesShipping = this.getSalesShippingService().get(
				salesShipping.getId());
		this.slist = salesShipping.getDetails();
		this.displayEndNodes(salesShipping);
//		for(SalesShippingDetail detail:salesShipping.getDetails()){
//			String bath=detail.getDetail();
//			String[] battchs=bath.split(",");
//			for(String string:battchs){
//				String[] asdfString=string.split("p");
//				System.out.println(asdfString[0]+"         "+asdfString[1]);
//			}
//		}
		return SUCCESS;
	}

	@Action(value = "checkstock", results = { @Result(name = "success", type = "json", params = {
			"root", "states" }) })
	public String checkstock() {
		List<ProductStore> productStores = this.getProductStoreService()
				.getProductStores(product, room);
		Double number = 0.0;
		for (ProductStore store : productStores) {
			number = number + store.getProductNum();
		}
		String OverNum = number.intValue() + "";
		if(number.intValue()==0){
			OverNum="5.4.45.45.454.544";
		}
		this.states = OverNum;
		return SUCCESS;
	}

	@Action("selectSalesShipping")
	public String selectSalesShipping() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SalesContract.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("flowstatus", 2));
		criteria.add(Restrictions.eq("returnState", 0));
		criteria.add(Restrictions.eq("ShippingState", 1));
		this.setPagination(this.getSalesShippingService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

//	@Action("selectSalesShippingProduct")
//	public String selectSalesShippingProduct() {
//		this.Detailist = this.getSalesShippingService().getAllDetails();
//
//		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
//				SalesShippingDetail.class);
//		criteria.add(Restrictions.eq("room", salesShippingDetail.getRoom()));
//		criteria.add(Restrictions.eq("salesShipping", salesShippingDetail
//				.getSalesShipping()));
//		criteria.add(Restrictions.eq("outState", 0));
//		pagination = this.getPaginationHelper().prepare(pagination);
//		this.setPagination(this.getSalesShippingService().listByPage(criteria,
//				pagination));
//		return SUCCESS;
//	}

	public SalesShipping getSalesShipping() {
		return salesShipping;
	}

	public void setSalesShipping(SalesShipping salesShipping) {
		this.salesShipping = salesShipping;
	}

	public SalesShippingService getSalesShippingService() {
		return salesShippingService;
	}

	public void setSalesShippingService(
			SalesShippingService salesShippingService) {
		this.salesShippingService = salesShippingService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public List<GatheringWay> getGatheringWaylist() {
		return gatheringWaylist;
	}

	public void setGatheringWaylist(List<GatheringWay> gatheringWaylist) {
		this.gatheringWaylist = gatheringWaylist;
	}

	public List<ShippingWay> getShippingWaylist() {
		return shippingWaylist;
	}

	public void setShippingWaylist(List<ShippingWay> shippingWaylist) {
		this.shippingWaylist = shippingWaylist;
	}

	public List<InvoiceType> getInvoiceTypelist() {
		return invoiceTypelist;
	}

	public void setInvoiceTypelist(List<InvoiceType> invoiceTypelist) {
		this.invoiceTypelist = invoiceTypelist;
	}

	public GatheringWayService getGatheringWayService() {
		return gatheringWayService;
	}

	public void setGatheringWayService(GatheringWayService gatheringWayService) {
		this.gatheringWayService = gatheringWayService;
	}

	public ShippingWayService getShippingWayService() {
		return shippingWayService;
	}

	public void setShippingWayService(ShippingWayService shippingWayService) {
		this.shippingWayService = shippingWayService;
	}

	public InvoiceTypeService getInvoiceTypeService() {
		return invoiceTypeService;
	}

	public void setInvoiceTypeService(InvoiceTypeService invoiceTypeService) {
		this.invoiceTypeService = invoiceTypeService;
	}

	public String getStates() {
		return states;
	}

	public void setStates(String states) {
		this.states = states;
	}

	public String getProName() {
		return proName;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public String[] getPronames() {
		return pronames;
	}

	public void setPronames(String[] pronames) {
		this.pronames = pronames;
	}

	public Double[] getOriginalCosts() {
		return originalCosts;
	}

	public void setOriginalCosts(Double[] originalCosts) {
		this.originalCosts = originalCosts;
	}

	public Double[] getNowCosts() {
		return nowCosts;
	}

	public void setNowCosts(Double[] nowCosts) {
		this.nowCosts = nowCosts;
	}

	public Integer[] getReturnsNums() {
		return returnsNums;
	}

	public void setReturnsNums(Integer[] returnsNums) {
		this.returnsNums = returnsNums;
	}

	public Double[] getRebates() {
		return rebates;
	}

	public void setRebates(Double[] rebates) {
		this.rebates = rebates;
	}

	public Double[] getTotalPrices() {
		return totalPrices;
	}

	public void setTotalPrices(Double[] totalPrices) {
		this.totalPrices = totalPrices;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public List<StoreRoom> getRoomlist() {
		return roomlist;
	}

	public void setRoomlist(List<StoreRoom> roomlist) {
		this.roomlist = roomlist;
	}

	public StoreRoom getRoom() {
		return room;
	}

	public void setRoom(StoreRoom room) {
		this.room = room;
	}

	public SalesContract[] getConratcts() {
		return conratcts;
	}

	public void setConratcts(SalesContract[] conratcts) {
		this.conratcts = conratcts;
	}

	public Collection<SalesShippingDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<SalesShippingDetail> slist) {
		this.slist = slist;
	}

	public SalesShippingDetail getSalesShippingDetail() {
		return salesShippingDetail;
	}

	public void setSalesShippingDetail(SalesShippingDetail salesShippingDetail) {
		this.salesShippingDetail = salesShippingDetail;
	}

	public List<SalesShippingDetail> getDetailist() {
		return Detailist;
	}

	public void setDetailist(List<SalesShippingDetail> detailist) {
		Detailist = detailist;
	}

	public String getBatchNumber() {
		return batchNumber;
	}

	public void setBatchNumber(String batchNumber) {
		this.batchNumber = batchNumber;
	}

	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

}
